package com.yogee.topseller.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yogee.topseller.R;
import com.yogee.topseller.adapter.ViewPagerAdapter;
import com.yogee.topseller.utils.AppUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * Description：
 * Author：LiuYM
 * Date： 2017-04-22 14:22
 */

public class ImageViewPagerActivity extends Activity {
    public static final String IMAGES_LIST_EXTRA = "IMAGES_LIST_EXTRA";
    public static final String IMAGES_LIST_POSITION_EXTRA = "IMAGES_LIST_POSITION_EXTRA";

    private Toolbar mToolbar;
    private ViewPager mViewPager;
    private LinearLayout mLinearLayout;

    private TextView mTvDesc;

    private ViewPagerAdapter mPagerAdapter;
    private String[] mImages;
    private int mCurPosition;

    private String savePath;
    private String mCurImageUrl;

    private List<ImageView> mDots;//底部小圆点的集合

    private ImageView img_return;


    public static void start(Context context, String[] images, int selectedPosition) {
        Intent intent = new Intent(context, ImageViewPagerActivity.class);
        intent.putExtra(IMAGES_LIST_EXTRA, images);
        intent.putExtra(IMAGES_LIST_POSITION_EXTRA, selectedPosition);
        context.startActivity(intent);
    }

    public static void start(Context context, String image) {
        Intent intent = new Intent(context, ImageViewPagerActivity.class);
        String[] imageArr = new String[1];
        imageArr[0]= image;
        intent.putExtra(IMAGES_LIST_EXTRA, imageArr);
        intent.putExtra(IMAGES_LIST_POSITION_EXTRA, 0);
        context.startActivity(intent);
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_image_view_pager_layout);

        mImages = getIntent().getStringArrayExtra(IMAGES_LIST_EXTRA);
        int position = getIntent().getIntExtra(IMAGES_LIST_POSITION_EXTRA, 0);

        mToolbar = (Toolbar) findViewById(R.id.toolBar);
        mViewPager = (ViewPager) findViewById(R.id.viewPager);
        mLinearLayout = (LinearLayout) findViewById(R.id.viewpager_linerlayout);
        img_return = (ImageView) findViewById(R.id.img_return);
        img_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        initDots();

        mTvDesc = (TextView) findViewById(R.id.tv_desc);

        //沉浸式全屏模式
//        SystemBarHelper.immersiveStatusBar(this, 0);
//        SystemBarHelper.setHeightAndPadding(this, mToolbar);

        mTvDesc.setText("这是一个image");
//        setNumTitle(position);
//        setSupportActionBar(mToolbar);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        //滚动条
        mTvDesc.setMovementMethod(ScrollingMovementMethod.getInstance());


        mPagerAdapter = new ViewPagerAdapter(mImages, this);
        mViewPager.setOffscreenPageLimit(1);
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                mTvDesc.scrollTo(0, 0);
                String text = "这是一个pic";

//                if (TextUtils.isEmpty(text)) {
//                    mTvDesc.setVisibility(View.GONE);
//                } else {
//                    mTvDesc.setVisibility(View.VISIBLE);
//                    mTvDesc.setText(text);
//                }
            }

            @Override
            public void onPageSelected(int position) {
                setNumTitle(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        mPagerAdapter.setListener(new ViewPagerAdapter.OnViewPagerAdapterListener() {
            @Override
            public void onClickListener(View view, int position) {
                showOrHideToolbar();
                //showOrHideText();
            }

            @Override
            public void onLongClickListener(View view, int position) {
                showListDialog();
            }
        });

        mViewPager.setCurrentItem(mCurPosition);
        setCurrentDot(mCurPosition);

    }

    private void setCurrentDot(int position){
        if(mCurPosition==position){
            mDots.get(position).setImageResource(R.drawable.point_select);
        }else{
            mDots.get(mCurPosition).setImageResource(R.drawable.point_unselect);
            mDots.get(position).setImageResource(R.drawable.point_select);
        }
        mCurPosition = position;

    }

    private void initDots() {
        mDots = new ArrayList<ImageView>();//底部圆点集合的初始化
        for (int i=0;i<mImages.length;i++){//根据界面数量动态添加圆点
            ImageView imageView=new ImageView(this);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(AppUtil.dip2px(ImageViewPagerActivity.this,10),AppUtil.dip2px(ImageViewPagerActivity.this,10)));//设置ImageView的宽度和高度
            imageView.setPadding(5,5,5,5);//设置圆点的Padding，与周围的距离
            imageView.setImageResource(R.drawable.point_unselect);//设置图片
            mDots.add(imageView);//将该图片添加到圆点集合中
            mLinearLayout.addView(imageView);//将图片添加到LinearLayout中
        }
    }



    private void showListDialog(){
        Toast.makeText(this,"长按测试!", Toast.LENGTH_SHORT).show();
    }


    private void setNumTitle(int position) {
//        mCurPosition = position;
        mCurImageUrl=mImages[mCurPosition];
        setCurrentDot(position);
//        mToolbar.setTitle(position + 1 + "/" + mImages.length);
    }


    /**
     * 标题和文字的显示与隐藏
     */
    private void showOrHideText() {
//        if (mTvDesc.getVisibility() == View.VISIBLE) {
//            Animation tvTextAnimaBottom = AnimationUtils.loadAnimation(this, R.anim.anim_exit_from_bottom);
//
//            tvTextAnimaBottom.setAnimationListener(new Animation.AnimationListener() {
//                @Override
//                public void onAnimationStart(Animation animation) {
//                }
//
//                @Override
//                public void onAnimationEnd(Animation animation) {
//                    mTvDesc.setVisibility(View.GONE);
//                }
//
//                @Override
//                public void onAnimationRepeat(Animation animation) {
//
//                }
//            });
//            mTvDesc.startAnimation(tvTextAnimaBottom);
//
//        } else {
//            mTvDesc.setVisibility(View.VISIBLE);
//            mTvDesc.clearAnimation();
//            Animation textAnimaBottom = AnimationUtils.loadAnimation(this, R.anim.anim_enter_from_bottom);
//            mTvDesc.startAnimation(textAnimaBottom);
//
//        }
    }


    /**
     * 标题和文字的显示与隐藏
     */
    private void showOrHideToolbar() {
//        if (mToolbar.getVisibility() == View.VISIBLE) {
//            Animation titleAnimaTop = AnimationUtils.loadAnimation(this, R.anim.anim_exit_from_top);
//
//            titleAnimaTop.setAnimationListener(new Animation.AnimationListener() {
//                @Override
//                public void onAnimationStart(Animation animation) {
//
//                }
//
//                @Override
//                public void onAnimationEnd(Animation animation) {
//                    mToolbar.setVisibility(View.GONE);
//                }
//
//                @Override
//                public void onAnimationRepeat(Animation animation) {
//
//                }
//            });
//            mToolbar.startAnimation(titleAnimaTop);
//
//        } else {
//            mToolbar.setVisibility(View.VISIBLE);
//            mToolbar.clearAnimation();
//            Animation toolAnimaTop = AnimationUtils.loadAnimation(this, R.anim.anim_enter_from_top);
//            mToolbar.startAnimation(toolAnimaTop);
//
//        }
    }
}
